package com.gzsxy.sso.core.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzsxy.sso.common.dto.request.RoleRequest;
import com.gzsxy.sso.common.dto.response.MenuRsp;
import com.gzsxy.sso.common.dto.response.RoleFunctionRsp;
import com.gzsxy.sso.common.dto.response.UserRsp;
import com.gzsxy.sso.common.result.R;
import com.gzsxy.sso.core.entity.Role;
import com.gzsxy.sso.core.entity.User;
import com.gzsxy.sso.core.entity.vo.RoleQueryVo;
import com.gzsxy.sso.core.entity.vo.UserQueryVo;
import com.gzsxy.sso.core.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理
 * @author zxl
 * @since 2021-11-10
 */
@Api(description = "角色管理")
@CrossOrigin  //解决跨域
@RestController
@RequestMapping("/admin/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @ApiOperation("查询所有的角色")
    @GetMapping("/getAllRole")
    public R getAllRole(){
        List<Role> allRoles = roleService.getAllRoles();
        return R.ok().message("查询成功").data("data",allRoles);
    }



    @ApiOperation("查询所有角色下所有功能列表")
    @GetMapping("/getAll")
    public R findFunctionByRoles(){
        List<RoleFunctionRsp> list = roleService.findFunctionByRoles();
        return R.ok().message("查询成功").data("data",list);
    }



    @ApiOperation("角色分页列表查询")
    @GetMapping("/list")
    public R searchPageRole(@ApiParam(value = "当前页面",required = true,defaultValue ="1" ) @RequestParam Long page,         //long数据类型适用于较大整数类型
                            @ApiParam(value = "每页记录数",required = true,defaultValue ="5") @RequestParam Long limit,
                            @ApiParam("查询对象") RoleQueryVo roleQueryVo){
        IPage<?> pageParam = new Page<>(page, limit);
        IPage<RoleFunctionRsp> rspIPage = roleService.selectPage(pageParam,roleQueryVo);

        //获取查询的总结果
        List<RoleFunctionRsp> records = rspIPage.getRecords();
        //总记录数
        long total = rspIPage.getTotal();
        //之所以ok（）方法后面还可以加方法是因为ok方法返回是r对象，对象又可以调用他的方法
        return R.ok().data("total",total).data("row",records);
    }



    @ApiOperation("添加角色")
    @PostMapping("/addRole")
    public R addRole(@RequestBody RoleRequest roleRequest){
        roleService.addRole(roleRequest);
        return R.ok().message("添加成功");
    }


    @ApiOperation("修改角色")
    @PutMapping("/update")
    public R updateRole(@RequestBody RoleRequest roleRequest){
        roleService.updateRole(roleRequest);
        return R.ok().message("修改成功");
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/delete/{roleId}")
    public R deleteRole(@PathVariable String roleId){
        roleService.deleteRole(roleId);
        return R.ok().message("删除成功");
    }


    @ApiOperation("根据id查询角色")
    @GetMapping("/{roleId}")
    public R selectById(@PathVariable String roleId){
        Role role = roleService.selectById(roleId);
        return R.ok().message("查询成功").data("data",role);
    }


}

